namespace GemBox.Spreadsheet
{
    using System;
    using System.Collections;
    using System.Drawing;
    using System.IO;
    using System.Reflection;

    public class ExcelPictureCollection : IEnumerable
    {
        internal ArrayList items;
        private ExcelWorksheet worksheet;

        internal ExcelPictureCollection(ExcelWorksheet worksheet)
        {
            this.worksheet = worksheet;
            this.items = new ArrayList();
        }

        internal ExcelPictureCollection(ExcelWorksheet worksheet, ExcelPictureCollection sourcePictures) : this(worksheet)
        {
            this.items = sourcePictures.items;
        }

        internal void Add(Image image, MsoBlipType blipType)
        {
            this.AddImage(image);
            this.Worksheet.Shapes.Add(image, blipType);
        }

        public void Add(string fileName, Rectangle rect)
        {
            this.Add(Utilities.CopyFileStreamFromFileName(fileName), rect, ExcelPicture.GetExcelPictureFormatFromFileName(fileName));
        }

        internal void Add(Image image, Rectangle rect, MsoBlipType blipType)
        {
            this.AddImage(image, rect);
            this.Worksheet.Shapes.Add(image, rect, blipType);
        }

        public void Add(MemoryStream imageStream, Rectangle rect, ExcelPictureFormat format)
        {
            ExcelPicture picture = new ExcelPicture(this, this.items.Count, imageStream, rect, format);
            this.items.Add(picture);
            this.Worksheet.Shapes.Add(picture.Image, rect);
            picture.SetAnchorFromRectangle();
        }

        public void Add(string fileName, PositioningMode mode, AnchorCell topLeftCell, AnchorCell bottomRightCell)
        {
            this.Add(Utilities.CopyFileStreamFromFileName(fileName), mode, topLeftCell, bottomRightCell, ExcelPicture.GetExcelPictureFormatFromFileName(fileName));
        }

        public void Add(MemoryStream imageStream, PositioningMode mode, AnchorCell topLeftCell, AnchorCell bottomRightCell, ExcelPictureFormat format)
        {
            ExcelPicture picture = new ExcelPicture(this, this.items.Count, imageStream, format);
            this.items.Add(picture);
            this.Worksheet.Shapes.Add(picture.Image);
            picture.SetAnchor(mode, topLeftCell, bottomRightCell);
        }

        private void AddImage(Image image)
        {
            Rectangle boundingRectangle = new Rectangle(0, 0, image.Width, image.Height);
            this.items.Add(new ExcelPicture(this, this.items.Count, image, boundingRectangle));
        }

        private void AddImage(Image image, Rectangle rect)
        {
            this.items.Add(new ExcelPicture(this, this.items.Count, image, rect));
        }

        public void Clear()
        {
            this.items.Clear();
        }

        internal void DeleteInternal(int index)
        {
            this.items.RemoveAt(index);
            this.FixAllIndexes(index, -1);
            this.worksheet.Shapes.DeleteInternal(index);
        }

        private void FixAllIndexes(int index, int offset)
        {
            for (int i = index; i < this.items.Count; i++)
            {
                ExcelPicture picture = (ExcelPicture) this.items[i];
                picture.Index += offset;
            }
        }

        public IEnumerator GetEnumerator()
        {
            return this.items.GetEnumerator();
        }

        public int Count
        {
            get
            {
                return this.items.Count;
            }
        }

        public ExcelPicture this[int index]
        {
            get
            {
                return (this.items[index] as ExcelPicture);
            }
        }

        internal ExcelWorksheet Worksheet
        {
            get
            {
                return this.worksheet;
            }
        }
    }
}
